package com.yige.web.controller

import com.mongodb.BasicDBObject
import com.mongodb.DBObject
import com.yige.common.DateUtil
import com.yige.common.ResultUtil
import com.yige.common.ToDBObjectUtil
import com.yige.service.mongoService.MongoExpenditureService
import com.yige.service.mongoService.MongoSalaryService
import com.yige.service.mongoService.MongoTeacherService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.util.StringUtils
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody

/**
 * Created by sunhao on 2015/9/30 0030.
 */

@Controller
@RequestMapping("salary")
class SalaryController {

    @Autowired
    MongoSalaryService salaryService
    @Autowired
    MongoTeacherService teacherService
    @Autowired
    MongoExpenditureService expenditureService

    /**
     * 已发工资页面
     * @param model
     * @return
     */
    @RequestMapping("list.do")
    def list(Model model){
        List<DBObject> list = expenditureService.findListByQuery([action : "发教师工资"]as BasicDBObject)
        List<Map<String, Object>> salaryList = new LinkedList<>()
        Map<String, Object> salary = new HashMap<>()
        DBObject teacher
        list.each {
            teacher = teacherService.findOneById(it.teacher)
            salary.put("_id",it._id)
            salary.put("date",DateUtil.formatDate(it.date))
            salary.put("teacher",teacher.name)
            salary.put("pay",it.pay)
            salary.put("info",it.description)
            salaryList.add(salary)
            salary = new HashMap<>()
        }
        model.addAttribute("salary",salaryList)
        return "salary/list"
    }

    @RequestMapping("listyingfa.do")
    def list0(Model model){
        List<DBObject> list = salaryService.findList()
        List<Map<String, Object>> salaryList = new LinkedList<>()
        Map<String, Object> salary = new HashMap<>()
        DBObject teacher
        list.each {
            teacher = teacherService.findOneById(it.teacher)
            salary.put("_id",it._id)
            salary.put("teacher",teacher.name)
            salary.put("pay",it.pay)
            salary.put("info",it.info)
            salaryList.add(salary)
            salary = new HashMap<>()
        }
        model.addAttribute("salary",salaryList)
        return "salary/salaryList"
    }

    /**
     * 添加工资表信息
     * @param teacher
     * @param pay
     * @param startDate
     * @param endDate
     * @return
     */
    @RequestMapping("addSalary")
    @ResponseBody
    def addSalary(String teacher,Double pay,String startDate, String endDate){
        boolean flag = false
        if(!StringUtils.isEmpty(teacher) && !StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
            def salary = [_id:UUID.randomUUID().toString(),
                          teacher:teacher,
                          pay:pay,
                          info:startDate + "至" + endDate] as HashMap
            DBObject dbObject = ToDBObjectUtil.toDBObject(salary)
            dbObject.put("date",new Date(System.currentTimeMillis()))
            teacherService.addSalaryForTeacher(teacher,dbObject._id)
            flag = salaryService.insertOne(dbObject)
        }
        ResultUtil.recallAjax(flag)
    }

    /**
     * 确认工资发放
     * @param _ids
     * @return
     */
    @RequestMapping("faSalary")
    @ResponseBody
    def delSalary(@RequestParam('_ids') List<String> _ids){
        boolean flag = false
//        DBObject teacher
        _ids.each {_id->
//            teacher = teacherService.findOneByQuery([salary:[$in:[_id]]as BasicDBObject]as BasicDBObject)
//            teacherService.removeSalaryForTeacher(teacher._id,_id)
            flag = salaryService.deleteOne(_id)
        }
        ResultUtil.recallAjax(flag)
    }

}
